package bsc.war.form;

import bsc.ejb.entity.DimensionalCut;
import bsc.ejb.entity.Pointer;
import bsc.war.delegate.DelegateBSC;
import bsc.war.factory.Factory;
import bsc.war.service.ScoreCardUtils;
import java.util.List;
import java.util.Map;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import org.springframework.web.servlet.ModelAndView;
import org.springframework.web.servlet.mvc.AbstractController;

/**
 *
 * @author Miguel Angel
 */
public class DeletePointer extends AbstractController {

    @Override
    protected ModelAndView handleRequestInternal(HttpServletRequest request,
            HttpServletResponse response) throws Exception {

        try {
            DelegateBSC delegate = (DelegateBSC) Factory.getBean("DelegateBSC");
            Long idScoreCard = Long.valueOf(request.getParameter("id"));
            Pointer pointer = (Pointer) delegate.findScoreCard(idScoreCard);

            List<DimensionalCut> dimensionalCutsRelation = ScoreCardUtils.hasPointerRelation(delegate, pointer);


            boolean deleted = true;
            if (dimensionalCutsRelation.size() > 0) {
                deleted = false;
            } else {
                ScoreCardUtils.deleteScoreCard(request, response, delegate, pointer);
            }

            //Se obtiene los strings de los cuts
            Map<DimensionalCut, String> dimensionalCutsMembers = ScoreCardUtils.getDimensionalCutsMembersStrings(request, delegate, dimensionalCutsRelation);

            
            request.setAttribute("dimensionalCutsMembers", dimensionalCutsMembers);
            request.setAttribute("dimensionalCutsRelation", dimensionalCutsRelation);
            request.setAttribute("deleted", deleted);

        } catch (Exception e) {
            e.printStackTrace();
        }

        return new ModelAndView("deletePointer");
    }
}
